package com.school.entity;

import java.util.ArrayList;
import java.util.List;

//学生子类：继承Person
public class Student extends Person {
    //学生专属属性
    private String studentId;
    private List<Course> selectedCourses; //存储已选课程

    //子类构造方法：初始化父类属性
    public Student(String id, String name, String gender, String studentId) {
        super(id, name, gender);
        this.studentId = studentId;
        this.selectedCourses = new ArrayList<>();
    }

    //学生选课
    public boolean selectCourse(Course course) {
        // 避免重复选课
        for (Course c : selectedCourses) {
            if (c.getId().equals(course.getId())) {
                return false;
            }
        }
        selectedCourses.add(course);
        return true;
    }

    //学生退课
    public boolean dropCourse(String courseId) {
        for (Course c : selectedCourses) {
            if (c.getId().equals(courseId)) {
                selectedCourses.remove(c);
                return true;
            }
        }
        return false; // 未选该课程，退课失败
    }

    //打印学生课表
    @Override
    public void printWorksheet() {
        System.out.println("=== 学生课表 ===");
        super.printBaseInfo();
        System.out.println("学号：" + this.studentId);
        System.out.println("已选课程：");

        if (selectedCourses.isEmpty()) {
            System.out.println("  暂无选课");
        } else {
            for (int i = 0; i < selectedCourses.size(); i++) {
                Course c = selectedCourses.get(i);
                System.out.printf("  %d. 课程名：%s | 地点：%s | 时间：%s | 授课教师：%s%n",
                        i + 1, c.getName(), c.getLocation(), c.getTime(), c.getTeacherName());
            }
        }
        System.out.println("================\n");
    }

    // Getter：供system包访问选课列表
    public List<Course> getSelectedCourses() {
        return selectedCourses;
    }

    public String getStudentId() {
        return studentId;
    }
}